<?php
namespace Api\Controller;
use Think\Controller;
use Think\Vender;
class PayController extends Controller {
    public function index()
    {
        echo "pay"; 
    }
   
    public function pay(){
        $order_no = I('post.order_no');
        Vendor("XcxPay.XcxPay");
        $weixinpay = new \XcxPay($order_no);
        $ret['pay_info']=$weixinpay->pay();

        $ret['order'] = M('order')->where('order_no = "'.$order_no.'"')->find();
        $ret['isSuccess'] = 1;
        ret($ret);
    }

    public function notify(){
        //ini_set('max_execution_time', '0');
        $xml = file_get_contents('php://input');
        libxml_disable_entity_loader(true);
        $data = json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA));
        // $huidiao['content'] = $data;
        // $huidiao['time'] = date('Y-m-d H:i:s',time());
        // M('huidiao')->add($huidiao); //这些是我收到微信的通知，存到库里方便看的
        $data = json_decode($data,true);
        // $huidiao['content'] = $data;
        // $huidiao['time'] = date('Y-m-d H:i:s',time());
        // M('huidiao')->add($huidiao); //这些是我收到微信的通知，存到库里方便看的


        $replyData['return_msg'] = "";
        $replyData['return_code'] = $data['return_code'];
        
        if(strtolower($data['result_code']) == 'success' && strtolower($data['return_code']) == 'success'){
            $order = M('order')->where('order_no = "'.$data['out_trade_no'].'"')->find();
            //你到这儿已经取得了这个订单了 被付款的这个订单

            // $user = M('user')->where('id = '.$order['user_id'])->find();
            // if($order['state'] == 1){
            //     if($order['pay_money'] == $data['total_fee']/100){
            //         $this->cut_fee($order);
            //         $this->send_fee($order);
            //         $o['state'] = 2;
            //         $o['pay_time'] = time();
            //         M('order')->where('id = "'.$order['id'].'"')->save($o);
            //         if ($order['type'] == 1) {
            //             $this->do_worry($order);
            //         }else if ($order['type'] == 2) {
            //             $this->do_top($order);
            //         }else if ($order['type'] == 3) {
            //             $this->do_send($order);
            //         }else if ($order['type'] == 4) {
            //             $this->do_vip($order);
            //         }
            //     }
            //     
            // }
            //这些是我的业务逻辑

            $replyData['return_msg'] = "OK";
        }
        echo $this->arrayToXml($replyData);
    }

}